"""
#!/usr/bin/python3
# coding:utf-8            
#
# Copyright (C) 2024 - 2024 Jasonakeke， Inc. All Rights Reserved 
# @Desc	   ：
# @Time    : 2024/8/9 22:19
# @Author  : Code_By_Jasonakeke
# @Email   : 2284037977@qq.com
# @File    : 02-cookie.py
# @IDE     : PyCharm
"""
from json import JSONEncoder
from os import urandom
from pickle import dumps, loads

from flask import Flask, make_response, request, session

app = Flask(__name__)


class MyJsonEncoder(JSONEncoder):
	def default(self, obj):
		if isinstance(obj, User):
			return {
				'username': obj.un,
				'password': obj.pwd
			}
		else:
			return JSONEncoder.default(self, obj)


app.json_encoder = MyJsonEncoder


# 设置 SECRET_KEY
class DefaultConfig(object):
	SECRET_KEY = urandom(24)


app.config.from_object(DefaultConfig)


@app.route('/set')
def hello_world():
	session['un'] = 'Jasonkeke'
	session['pwd'] = '123456'
	session['user'] = User('Jasonkeke', '123456')
	return '设置 cookie 的内容'


@app.route('/get')
def get():
	un = session.get('un')
	pwd = session.get('pwd')
	user = session.get('user')
	print(un, pwd, user)
	return 'un: %s, pwd: %s, user: %s' % (un, pwd, user)


class User(object):
	def __init__(self, un, pwd):
		self.un = un
		self.pwd = pwd


if __name__ == '__main__':
	app.run(debug = True)
